package com.mmall.controller.backend;

import com.mmall.common.ServerResponse;
import com.mmall.pojo.Product;
import com.mmall.service.IFileService;
import com.mmall.service.IProductService;
import com.mmall.util.PropertyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by root on 18-4-7.
 */
@Controller
@RequestMapping("/manage/product/")
public class ProductManageController {

    @Autowired
    private IProductService productService;

    @Autowired
    private IFileService fileService;

    @RequestMapping("save.do")
    @ResponseBody
    public ServerResponse saveProduct(HttpSession session, Product product){
        ServerResponse resp = UserHelper.validAdmin(session);
        if (!resp.isSuccess()){
            return resp;
        }
        return productService.saveProduct(product);
    }

    @RequestMapping("set_sale_status.do")
    @ResponseBody
    public ServerResponse setSaleStatus(HttpSession session, Integer productId, String status){
        ServerResponse resp = UserHelper.validAdmin(session);
        if (!resp.isSuccess()){
            return resp;
        }
        return productService.setSaleStatus(productId, status);
    }

    @RequestMapping("detail.do")
    @ResponseBody
    public ServerResponse getProductDetail(HttpSession session, Integer productId){
        ServerResponse resp = UserHelper.validAdmin(session);
        if (!resp.isSuccess()){
            return resp;
        }
        return productService.getProductDetail(productId);
    }


    @RequestMapping("list.do")
    @ResponseBody
    public ServerResponse getProductList(HttpSession session, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10")Integer pageSize){
        ServerResponse resp = UserHelper.validAdmin(session);
        if (!resp.isSuccess()){
            return resp;
        }
        return productService.getManageProductList(pageNum, pageSize);
    }


    @RequestMapping("search.do")
    @ResponseBody
    public ServerResponse productSearch(HttpSession session, String productName, Integer productId, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10")Integer pageSize){

        ServerResponse resp = UserHelper.validAdmin(session);
        if (!resp.isSuccess()){
            return resp;
        }
        return productService.searchProductList( productName,  productId,  pageNum, pageSize);
    }


    @RequestMapping(value = "upload.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse upload(MultipartFile file, HttpServletRequest request){
        String uploadPath = request.getServletContext().getRealPath("upload");
        String uploadFileName = fileService.upload(file, uploadPath);
        String fileUrl = PropertyUtil.getFtpHost() + uploadFileName;
        Map rsMap = new HashMap<String, String>();
        rsMap.put("uri", uploadFileName);
        rsMap.put("url", fileUrl);
        return ServerResponse.createBySuccess(rsMap);
    }


}


